/*
 *  camerarecorder.cpp
 *  opencvTest
 *
 *  Created by a1gucis on 2/26/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#include "camerarecorder.h"

CameraRecorder::CameraRecorder(string fileName) {
	this->fileName = fileName;
}

void CameraRecorder::record() {
	cvNamedWindow("Output", CV_WINDOW_AUTOSIZE);
	CvCapture* capture = 0; 
    //capture = cvCaptureFromCAM(0);
	capture = cvCaptureFromCAM(0);
    if(capture == NULL){ 
		cout<<"Error with capture"<<endl;
        return; 
    } 
    IplImage *bgr_frame;//=cvQueryFrame(capture);//Init the video read 
    double fps = 25.0; //= cvGetCaptureProperty (capture, CV_CAP_PROP_FPS);
	//CvSize size = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)); 
    CvSize size = cvSize(640, 480);
	CvVideoWriter *writer = cvCreateVideoWriter(fileName.c_str(), CV_FOURCC('M','J','P','G'), fps, size); 
    IplImage* logpolar_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); 
	
	char c = 0;
	while (c != 27) {
		bgr_frame=cvQueryFrame(capture);
		cvShowImage("Output", bgr_frame);
		c = cvWaitKey(25);
	}
	c = 0;
	while(c != 27) { 
		bgr_frame=cvQueryFrame(capture);
		cvFlip(bgr_frame, NULL, 1);
		if (bgr_frame != NULL) {
			//cvLogPolar( bgr_frame, logpolar_frame, cvPoint2D32f(bgr_frame->width/2, bgr_frame->height/2), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS); 
			//cvWriteFrame( writer, logpolar_frame ); 
			cvWriteFrame(writer, bgr_frame);
		}
		cvShowImage("Output", bgr_frame);
		c = cvWaitKey(25);
    } 
	cvDestroyWindow("Output");
    cvReleaseVideoWriter( &writer ); 
    cvReleaseImage( &logpolar_frame ); 
    cvReleaseCapture( &capture ); 
    return; 
	
	
}